import { auth } from "../../libs/lucia";
import type { APIRoute } from 'astro';

export const POST: APIRoute = async (context) => {
	const session = await context.locals.auth.validate();
	if (!session) {
		return new Response("Unauthorized", {
			status: 401
		});
	}
	// make sure to invalidate the current session!
	await auth.invalidateSession(session.sessionId);
	// delete session cookie
	context.locals.auth.setSession(null);
	const { params: { locale } } = context;
	return context.redirect("/" + locale + "/login", 302);
}